package game.msgame.dao;

import game.msgame.constant.DATACONSTANT;

/**
 * 聊天数据数据类
 * 负责提供对私聊消息、群发消息的操作服务
 *
 * @author Mr.Alpaca
 * @version 1.0.0
 */
public class DataDao {
    /**
     * 单例对象,懒加载
     */
    private static DataDao dao;

    private DataDao() {
    }

    /**
     * 获取单例对象,使用DCL机制
     *
     * @return 单例对象
     */
    public static DataDao getDao() {
        if (dao == null) {
            synchronized (DataDao.class) {
                if (dao == null) {
                    dao = new DataDao();
                }
            }
        }
        return dao;
    }

    /**
     * 根据传入参数查找历史消息
     *
     * @param id 对话双方用户id
     * @return 历史消息
     */
    public String selectHistory(String id) {
        StringBuilder builder = DATACONSTANT.HISTORY_MESSAGE_MAP.get(id);
        return builder != null ? builder.toString() : "";
    }

    /**
     * 添加记录到指定索引下
     *
     * @param id      索引
     * @param message 消息记录
     */
    public void insertMessage(String id, String message) {
        StringBuilder builder = DATACONSTANT.HISTORY_MESSAGE_MAP.get(id);
        if (builder == null) {
            builder = new StringBuilder();
        }
        builder.append(message);
        DATACONSTANT.HISTORY_MESSAGE_MAP.put(id, builder);
    }
}
